package org.bronycorps.mlptss.domain.layer;

import org.bronycorps.mlptss.domain.controller.GameController;
import org.bronycorps.mlptss.domain.model.Player;
import org.bronycorps.mlptss.domain.model.type.PlayerType;
import org.bronycorps.mlptss.screen.LevelScreen;

public class ActorLayer extends AbstractLayer {

	private Track[] track = new Track[2];

	public ActorLayer() {
		super();

		this.track[0] = new Track();
		this.track[0].setWidth(LevelScreen.STAGE_WIDTH);
		this.track[0].setHeight(300);
		this.track[0].setY(300);
		this.track[1] = new Track();
		this.track[1].setWidth(LevelScreen.STAGE_WIDTH);
		this.track[1].setHeight(300);

		addActor(this.track[0]);
		addActor(this.track[1]);

		GameController gc = GameController.getInstance();

		createPlayer(gc.getPlayer(0));
		createPlayer(gc.getPlayer(1));

	}

	private void createPlayer(Player player) {
		int playerId = player.getIdPlayer();
		if (player.getPlayerType() == PlayerType.COMPUTER) {
			this.track[playerId].createComputerPlayer(player);
		} else {
			this.track[playerId].createHumanPlayer(player);
		}
	}

	public Track getOppositeTrackFrom(Track track) {
		if (this.track[0] == track) {
			return this.track[1];
		} else {
			return this.track[0];
		}
	}

	public Track getPlayerTrack(Player player) {
		if (this.track[0].getPlayer().getModel().equals(player)) {
			return this.track[0];
		} else {
			return this.track[1];
		}
	}

	public Track getTrack(int idx) {
		return this.track[idx];
	}

	public Track[] getTracks() {
		return this.track;
	}

	public void startGame() {
		int initialIdWave = GameController.getInstance().getWaveFactory()
				.getRandomWaveId();

		for (Track t : this.track) {
			t.StartGame(initialIdWave);
		}
	}

}
